// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CourseAction.java

package com.iphaeton.lms.action.course.self;

import com.iphaeton.common.helper.*;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.action.course.scorm12.CourseBizSupportAction;
import com.iphaeton.lms.business.course.scorm12.ICourseService;
import com.iphaeton.lms.business.course.scorm12.IItemService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.pojo.course.Course;
import com.iphaeton.lms.pojo.course.Item;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.dom4j.io.XMLWriter;

public class CourseAction extends CourseBizSupportAction
{

    public CourseAction()
    {
    }

    public ActionForward courseForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        java.util.List list;
        try
        {
            list = course.findCourseSort();
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("list", list);
        return mapping.findForward("course_create");
    }

    public ActionForward createCourse(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String name = (String)daf.get("name");
        String theWebPath = request.getRealPath("/");
        String sort = (String)daf.get("sort");
        Integer hour = (Integer)daf.get("period");
        Course c = new Course();
        c.setName(name);
        c.setHour(hour);
        c.setState("a");
        c.setCriterion("\u81EA\u5236\u8BFE\u7A0B");
        c.setSortId(sort);
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String createrId = userSession.getUserId();
            String organize_id = userSession.getOrgId();
            c.setOrganizeId(organize_id);
            c.setCreater(createrId);
            c = course.persistentCommit(c);
            course.assignCourse(c, organize.findOrganizeById(organize_id));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("course", c);
        return mapping.findForward("course_catalog");
    }

    public ActionForward addItem(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String id = request.getParameter("id");
        String item_id = request.getParameter("item_id");
        String title = (String)daf.get("title");
        FormFile content = (FormFile)daf.get("content");
        FormFile text = (FormFile)daf.get("text");
        String contentName = content.getFileName();
        String textName = text.getFileName();
        String theWebPath = request.getRealPath("/");
        String filename = theWebPath + "course" + File.separator + id + File.separator;
        try
        {
            Course c = course.findCourseById(id);
            Item i = new Item();
            i.setTitle(title);
            i.setCourseId(id);
            i = item.save(i);
            item.addItem(c, i, item_id, contentName, textName);
            if(!"".equals(textName))
                upload.upload(text, filename + i.getId() + File.separator);
            if(!"".equals(contentName))
                upload.upload(content, filename + i.getId() + File.separator);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        return mapping.findForward("ok");
    }

    public ActionForward courseTreeManage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        org.dom4j.Document document = null;
        String id = request.getParameter("id");
        String sn = request.getParameter("sn");
        try
        {
            document = item.getCourseTree(request.getContextPath(), id, sn);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(document);
        writer.flush();
        writer.close();
        return null;
    }

    private static Logger logger;
    private DynaActionForm daf;
    private Pagination pageInfo;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.course.self.CourseAction.class.getName());
    }
}
